//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CostFunction.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CCostFunction::CCostFunction (boost::shared_ptr<QuantLib::CostFunction>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCostFunction = NULL;
#endif
	_ppCostFunction = &childNative;
    
}
Cephei::QL::Math::Optimization::CCostFunction::CCostFunction (QuantLib::CostFunction& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCostFunction = NULL;
#endif
	_ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (&childNative);
    
    _CostFunctionOwner = owner;
    
}

Cephei::QL::Math::Optimization::CCostFunction::CCostFunction (CCostFunction^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCostFunction = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Optimization::CCostFunction::CCostFunction (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCostFunction = NULL;
#endif
	if (!t->IsSubclassOf(CCostFunction::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CCostFunction::CCostFunction (QuantLib::Handle<QuantLib::CostFunction>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCostFunction = &childNative;
	_ppCostFunction = &static_cast<boost::shared_ptr<QuantLib::CostFunction>>(childNative.currentLink());
    
    _CostFunctionOwner = owner;
}
Cephei::QL::Math::Optimization::CCostFunction::CCostFunction (QuantLib::Handle<QuantLib::CostFunction> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCostFunction = &childNative;
	_ppCostFunction = &static_cast<boost::shared_ptr<QuantLib::CostFunction>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CCostFunction::CCostFunction (QuantLib::CostFunction childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCostFunction = NULL;
#endif
	_ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (new QuantLib::CostFunction (childNative));
    
}
#endif

Cephei::QL::Math::Optimization::CCostFunction::~CCostFunction ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCostFunction != NULL)
    {
	    delete _ppCostFunction;
        _ppCostFunction = NULL;
    }
}
Cephei::QL::Math::Optimization::CCostFunction::!CCostFunction ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCostFunction != NULL)
    {
	    delete _ppCostFunction;
    }
}
QuantLib::CostFunction& Cephei::QL::Math::Optimization::CCostFunction::GetReference ()
{
    if (_ppCostFunction == NULL) throw REFNEW NativeNullException ();
	return **_ppCostFunction;
}
boost::shared_ptr<QuantLib::CostFunction>& Cephei::QL::Math::Optimization::CCostFunction::GetShared ()
{
    if (_ppCostFunction == NULL) throw REFNEW NativeNullException ();
	return *_ppCostFunction;
}
QuantLib::CostFunction* Cephei::QL::Math::Optimization::CCostFunction::GetPointer ()
{
    if (_ppCostFunction == NULL) throw REFNEW NativeNullException ();
	return &**_ppCostFunction;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CostFunction>& Cephei::QL::Math::Optimization::CCostFunction::GetHandle ()
{
	if (_phCostFunction == NULL)
	{
		_phCostFunction = new Handle<QuantLib::CostFunction> (*_ppCostFunction);
	}
	return *_phCostFunction;
}
#endif
bool Cephei::QL::Math::Optimization::CCostFunction::HasNative () 
{
	return (_ppCostFunction != NULL);
}

Double Cephei::QL::Math::Optimization::CCostFunction::FiniteDifferenceEpsilon::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCostFunction)->finiteDifferenceEpsilon ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::ICostFunction^ Cephei::QL::Math::Optimization::CCostFunction::Gradient (Cephei::QL::Math::IArray^ grad, Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cgrad;
    CArray^ _Cx;
    try
    {
        _Cgrad = safe_cast<CArray^> (grad);
        _Cgrad->Lock();
        QuantLib::Array& _grad = static_cast<QuantLib::Array&> (_Cgrad->GetReference ()); 
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	(*_ppCostFunction)->gradient ( _grad,  _x );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgrad != nullptr) _Cgrad->Unlock();
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Double Cephei::QL::Math::Optimization::CCostFunction::Value (Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cx;
    try
    {
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCostFunction)->value ( _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Double Cephei::QL::Math::Optimization::CCostFunction::ValueAndGradient (Cephei::QL::Math::IArray^ grad, Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cgrad;
    CArray^ _Cx;
    try
    {
        _Cgrad = safe_cast<CArray^> (grad);
        _Cgrad->Lock();
        QuantLib::Array& _grad = static_cast<QuantLib::Array&> (_Cgrad->GetReference ()); 
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCostFunction)->valueAndGradient ( _grad,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgrad != nullptr) _Cgrad->Unlock();
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

